package com.dk.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dk.constant.RedisPermissionConstant;
import com.dk.mapper.MenuMapper;
import com.dk.mapper.RoleMenuMapper;
import com.dk.pojo.Menu;
import com.dk.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service//通过阿里巴巴的service注解,暴露接口
@org.springframework.stereotype.Service //spring实例化对象的注解
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Autowired
    private RedisTemplate redisTemplate;
    //查询
    @Override
    public List<Map<String, Object>> getInfo(Integer pid) {
        //先判断Redis中是否存在
        String key = RedisPermissionConstant.MENU_INFO_KEY +pid;
        String feild = RedisPermissionConstant.MENU_INFO_FIELD +pid;
        //如果存在 直接返回
        HashOperations operations = redisTemplate.opsForHash();
        Boolean b = operations.hasKey(key, feild);
        if (b){
        List<Map<String,Object>> mapList =(List<Map<String,Object>>)operations.get(key,feild);
        return mapList;
        }else{
            //不存在 去数据库中查询并新增到redis中
            List<Menu> list= menuMapper.selectByExample(null);
            List<Map<String,Object>> mapList= getMenuTree(list,0);
            operations.put(key,feild,mapList);
            return mapList;
        }
    }


    /*@Override
    public List<Map<String, Object>> getInfoZtree() {
        List<Menu> menuList =menuMapper.selectByExample(null);
        List<Map<String, Object>> mapList=getInfoZtree(menuList,0);
        return mapList;
    }*/

    @Override
    public List<Map<String, Object>> getMenuZtree() {
        List<Menu> list = menuMapper.selectByExample(null);
        List<Map<String, Object>> mapList = getMenuzTree(list, 0);
        return mapList;
    }

    //新增
    @Override
    public boolean add(Menu menu) {
        int i = menuMapper.insertSelective(menu);
        String key = RedisPermissionConstant.MENU_INFO_KEY;
        redisTemplate.delete(key);
        return i>0;
    }

    //修改
    @Override
    public Menu toUpdate(Integer mid) {
        return menuMapper.selectByPrimaryKey(mid);
    }

    @Override
    public boolean update(Menu menu) {
        int i = menuMapper.updateByPrimaryKeySelective(menu);
        String key = RedisPermissionConstant.MENU_INFO_KEY;
        redisTemplate.delete(key);
        return i>0;
    }
    //删除
    @Override
    public boolean deleteById(Integer mid) {
        Integer i = menuMapper.deleteByPrimaryKey(mid);
        String key = RedisPermissionConstant.MENU_INFO_KEY;
        redisTemplate.delete(key);
        return i > 0;
    }

    @Override
    public List<Map<String, Object>> getMenuZtreeChecked(Integer rid) {
        //查询权限树
        List<Menu> list = menuMapper.selectByExample(null);
        //查询属于当前rid的mid  中间表
        List<Integer> mids = roleMenuMapper.selectRoleMenuById(rid);
        //通过对比进行数据的拼接
        return getMenuZtreeToUpdate(list,0,mids);
    }

    private List<Map<String, Object>> getMenuZtreeToUpdate(List<Menu> list, Integer pid, List<Integer> mids) {
        //定义一个集合进行数据的接受
        List<Map<String, Object>> mapList = new ArrayList<>();
        //遍历数据
        for(int i = 0; i < list.size(); i++) {
            Map<String, Object> map = null;
            Menu menu = list.get(i);
            if(pid.equals(menu.getParentid())) {
                map = new HashMap<>();
                map.put("id",menu.getId());
                map.put("name",menu.getText());
                if(mids != null && mids.size() > 0 && mids.contains(menu.getId())){
                    map.put("checked",true);
                }

                map.put("children",getMenuZtreeToUpdate(list,menu.getId(),mids));
            }
            if(map != null) {
                mapList.add(map);
                List<Map<String, Object>> l = (List<Map<String, Object>>)map.get("children");
                if(l.size() <= 0) {
                    map.remove("children");
                }
            }
        }
        return mapList;
    }
    private List<Map<String, Object>> getMenuTree(List<Menu> list, Integer pid) {
        //定义一个集合进行数据的接受
        List<Map<String, Object>> mapList = new ArrayList<>();
        //遍历数据
        for(int i = 0; i < list.size(); i++) {
            Map<String, Object> map = null;
            Menu menu = list.get(i);
            if(pid.equals(menu.getParentid())) {
                map = new HashMap<>();
                map.put("id",menu.getId());
                map.put("text",menu.getText());
                map.put("url",menu.getUrl());
                map.put("nodes",getMenuTree(list,menu.getId()));
            }
            if(map != null) {
                mapList.add(map);
                List<Map<String, Object>> l = (List<Map<String, Object>>)map.get("nodes");
                if(l.size() <= 0) {
                    map.remove("nodes");
                }
            }
        }
        return mapList;
    }


    private List<Map<String, Object>> getMenuzTree(List<Menu> menuList, Integer id) {
        //定义容器
        List<Map<String, Object>> mapList = new ArrayList<>();
        //遍历menuList
        for (int i = 0; i < menuList.size(); i++) {
            Map<String, Object> map = null;
            Menu menu = menuList.get(i);
            //对比 判断当前的父级节点是否为id
            if (menu.getParentid() .equals(id)) {
                map = new HashMap<>();
                map.put("id",menu.getId());
                map.put("name",menu.getText());
                map.put("children",getMenuzTree(menuList,menu.getId()));
            }
            if (map != null) {
                List<Map<String, Object>> list = (List<Map<String, Object>>)map.get("children");
                if (list.size() <= 0){
                    map.remove("children");
                }
                mapList.add(map);
            }
        }
        return mapList;
    }

    @Override
    public List<String> findPermissionByPid(Integer pid) {
        return menuMapper.findPermissionByPid(pid);
    }
}
